{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7254a39c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   Apples  Bananas\n",
      "0       1        2\n",
      "1       3        6\n",
      "2       7        3\n",
      "3       4        5\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# 创建两个Series对象\n",
    "series_apples = pd.Series([1, 3, 7, 4])\n",
    "series_bananas = pd.Series([2, 6, 3, 5])\n",
    "\n",
    "# 将两个Series对象相加，得到DataFrame，并指定列名\n",
    "df = pd.DataFrame({ 'Apples': series_apples, 'Bananas': series_bananas })\n",
    "\n",
    "# 显示DataFrame\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "cd796712",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   orange  watermelon\n",
      "0       1           2\n",
      "1       2           5\n",
      "2       4           6\n",
      "3       6           7\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "series1=pd.Series([1,2,4,6])\n",
    "series2=pd.Series([2,5,6,7])\n",
    "df=pd.DataFrame({'orange':series1,'watermelon':series2})\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8d86f298",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: pandas in /root/miniconda3/lib/python3.9/site-packages (2.2.3)\n",
      "Requirement already satisfied: numpy>=1.22.4 in /root/miniconda3/lib/python3.9/site-packages (from pandas) (1.22.4)\n",
      "Requirement already satisfied: python-dateutil>=2.8.2 in /root/miniconda3/lib/python3.9/site-packages (from pandas) (2.9.0.post0)\n",
      "Requirement already satisfied: pytz>=2020.1 in /root/miniconda3/lib/python3.9/site-packages (from pandas) (2024.2)\n",
      "Requirement already satisfied: tzdata>=2022.7 in /root/miniconda3/lib/python3.9/site-packages (from pandas) (2024.2)\n",
      "Requirement already satisfied: six>=1.5 in /root/miniconda3/lib/python3.9/site-packages (from python-dateutil>=2.8.2->pandas) (1.16.0)\n",
      "\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager, possibly rendering your system unusable.It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv. Use the --root-user-action option if you know what you are doing and want to suppress this warning.\u001b[0m\u001b[33m\n",
      "\u001b[0m\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.3.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m25.2\u001b[0m\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "pip install pandas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "93246a61",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'pandas' has no attribute '_version_'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[6], line 2\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mpandas\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mpd\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[43mpd\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_version_\u001b[49m\n",
      "\u001b[0;31mAttributeError\u001b[0m: module 'pandas' has no attribute '_version_'"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "pd._version_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "9b2c02d0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    snake  hbc\n",
      "0    derf    2\n",
      "1     rfe    5\n",
      "2  wecfrw    2\n",
      "3      wf    5\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "pda={'snake':[\"derf\",\"rfe\",\"wecfrw\",\"wf\"],'hbc':[2,5,2,5]}\n",
    "aa=pd.DataFrame(pda)\n",
    "print(aa)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "df868746",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    sites  number\n",
      "0  Google       1\n",
      "1  Runoob       2\n",
      "2    Wiki       3\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "mydataset = {\n",
    "  'sites': [\"Google\", \"Runoob\", \"Wiki\"],\n",
    "  'number': [1, 2, 3]\n",
    "}\n",
    "\n",
    "myvar = pd.DataFrame(mydataset)\n",
    "\n",
    "print(myvar)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "098eb20e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    1\n",
      "1    2\n",
      "2    3\n",
      "3    4\n",
      "Name: 343, dtype: int64\n",
      "7    1\n",
      "8    2\n",
      "3    3\n",
      "5    4\n",
      "Name: A, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# 创建一个Series对象，指定名称为'A'，值分别为1, 2, 3, 4\n",
    "# 默认索引为0, 1, 2, 3\n",
    "series = pd.Series([1, 2, 3, 4], name='343')\n",
    "\n",
    "# 显示Series对象\n",
    "print(series)\n",
    "\n",
    "# 如果你想要显式地设置索引，可以这样做：\n",
    "custom_index = [7, 8, 3, 5]  # 自定义索引\n",
    "series_with_index = pd.Series([1, 2, 3, 4], index=custom_index, name='A')\n",
    "\n",
    "# 显示带有自定义索引的Series对象\n",
    "print(series_with_index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "c0e09cab",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_290/772184055.py:1: DeprecationWarning: The 'fastpath' keyword in pd.Series is deprecated and will be removed in a future version.\n",
      "  pandas.Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath=False)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Series([], dtype: object)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pandas.Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "8a5c384a",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_290/2185758905.py:1: DeprecationWarning: The 'fastpath' keyword in pd.Series is deprecated and will be removed in a future version.\n",
      "  pandas.Series(data=None, index=None, dtype=int, name=5465, copy=False, fastpath=False)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Series([], Name: 5465, dtype: int64)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pandas.Series(data=None, index=None, dtype=int, name=5465, copy=False, fastpath=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "c88678ba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.01\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "a=[0.01,0.04,0.25]\n",
    "pan=pd.Series(a)#根据索引筛选出特定的值\n",
    "print(pan[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "7d01832b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x    bear\n",
      "y     cat\n",
      "z     dog\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "a = [\"bear\", \"cat\", \"dog\"]\n",
    "myvar = pd.Series(a,index=[\"x\",\"y\",\"z\"])#手动设置索引\n",
    "print(myvar)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e0fa2ea6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "44     Google\n",
      "77     Runoob\n",
      "544      Wiki\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "sites = {44: \"Google\", 77: \"Runoob\", 544: \"Wiki\"}\n",
    "\n",
    "myvar = pd.Series(sites)\n",
    "\n",
    "print(myvar)#索引手动添加，类似字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "310f0f34",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1    Google\n",
      "5    Runoob\n",
      "Name: hfweyrv, dtype: object\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "sites = {1: \"Google\", 5: \"Runoob\", 3: \"Wiki\"}\n",
    "\n",
    "myvar = pd.Series(sites,index=[1,5],name='hfweyrv')\n",
    "\n",
    "print(myvar)#只需要字典中的一部分值，不是所有"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "501d1794",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1    Google\n",
      "2    Runoob\n",
      "dtype: object\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "count          2\n",
       "unique         2\n",
       "top       Google\n",
       "freq           1\n",
       "dtype: object"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "sites = {1: \"Google\", 2: \"Runoob\", 3: \"Wiki\"}\n",
    "\n",
    "myvar = pd.Series(sites, index = [1, 2])\n",
    "\n",
    "print(myvar)\n",
    "myvar.head(2)#返回前两行\n",
    "myvar.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "3bf5ca42",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "索引： Index(['a', 'b', 'c', 'd', 'e', 'f'], dtype='object')\n",
      "数据： [1 2 3 4 5 6]\n",
      "数据类型： int64\n",
      "前两行数据： a    1\n",
      "b    2\n",
      "dtype: int64\n",
      "元素加倍后： a     2\n",
      "b     4\n",
      "c     6\n",
      "d     8\n",
      "e    10\n",
      "f    12\n",
      "dtype: int64\n",
      "累计求和： a     1\n",
      "b     3\n",
      "c     6\n",
      "d    10\n",
      "e    15\n",
      "f    21\n",
      "dtype: int64\n",
      "缺失值判断： a    False\n",
      "b    False\n",
      "c    False\n",
      "d    False\n",
      "e    False\n",
      "f    False\n",
      "dtype: bool\n",
      "排序后的 Series： a    1\n",
      "b    2\n",
      "c    3\n",
      "d    4\n",
      "e    5\n",
      "f    6\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# 创建 Series\n",
    "data = [1, 2, 3, 4, 5, 6]\n",
    "index = ['a', 'b', 'c', 'd', 'e', 'f']\n",
    "s = pd.Series(data, index=index)\n",
    "\n",
    "# 查看基本信息\n",
    "print(\"索引：\", s.index)\n",
    "print(\"数据：\", s.values)\n",
    "print(\"数据类型：\", s.dtype)\n",
    "print(\"前两行数据：\", s.head(2))\n",
    "\n",
    "# 使用 map 函数将每个元素加倍\n",
    "s_doubled = s.map(lambda x: x * 2)\n",
    "print(\"元素加倍后：\", s_doubled)\n",
    "\n",
    "# 计算累计和\n",
    "cumsum_s = s.cumsum()\n",
    "print(\"累计求和：\", cumsum_s)\n",
    "\n",
    "# 查找缺失值（这里没有缺失值，所以返回的全是 False）\n",
    "print(\"缺失值判断：\", s.isnull())\n",
    "\n",
    "# 排序\n",
    "sorted_s = s.sort_values()\n",
    "print(\"排序后的 Series：\", sorted_s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8d5c7b8b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
